from rest_framework.serializers import ModelSerializer, IntegerField, BooleanField, Serializer
from erp_system.models import PermissionsModel, RolesModel
from .permission_serialzer import PermissionsSerialzer


class BaseRolesSerializer(ModelSerializer):
    '''
    普通的序列化的类，支持新增修改角色名，删除，查询
    '''
    permissions = PermissionsSerialzer(many=True, read_only=True)

    class Meta:
        model = RolesModel
        fields = '__all__'


class RolesPartialSerializer(ModelSerializer):
    '''
    用于，给某一个角色批量授权序列化
    '''

    class Meta:
        model = RolesModel
        fields = ['id', 'permissions']


class RoleSetPermissionSerializer(Serializer):
    '''
    给某一个角色单一授权，也可以取消单一授权
    '''
    # 角色id
    role_id = IntegerField(write_only=True, required=True)
    # 单一的权限id
    permission = IntegerField(write_only=True, required=True)
    # 是否新授予权限，还是取消
    is_create = BooleanField(write_only=True, required=True)
